<?php


namespace app\api\controller;


use think\Controller;
use think\Db;
use think\Request;

class Activate extends Controller
{
    //激活数据activate
    public function activate(Request $request){
        $param = $request->param();
        if (empty($param['device_code'])){
            exit(json_encode(['code'=>0,'msg'=>'缺少参数','data'=>[]]));
        }
        //判断用户是否已经注册
        $accountsInfoModel = Db::connect('db_config2')->table('accounts_info')->where(['RegisterMachine'=>$param['device_code']])->find();
        if ($accountsInfoModel){
            exit(json_encode(['code'=>1,'msg'=>'该设备已经注册','data'=>[]]));
        }

        $userActivateDevice = Db::connect('db_operatelog')->table('user_activate_device')->where(['device_code'=>$param['device_code'],'spreader_id'=>$param['spreader_id']])->find();
        if ($userActivateDevice){
            exit(json_encode(['code'=>1,'msg'=>'该设备已经激活过','data'=>[]]));
        }
        //插入数据
        Db::connect('db_operatelog')->table('user_activate_device')->insert([
            'device_code'=>$param['device_code'],
            'spreader_id'=>$param['spreader_id'],
            'add_time'=>date('Y-m-d H:i:s')
        ]);

        //更新激活报告
        $userActivateReport = Db::connect('db_operatelog')->table('user_activate_report')->where(['spreader_id'=>$param['spreader_id'],'date'=>date('Y-m-d')])->find();
        if ($userActivateReport){
            Db::connect('db_operatelog')->table('user_activate_report')->where(['spreader_id'=>$param['spreader_id'],'date'=>date('Y-m-d')])->setInc('number', 1);
        }else{
            Db::connect('db_operatelog')->table('user_activate_report')->insert([
                'spreader_id'=>$param['spreader_id'],
                'date'=>date('Y-m-d'),
                'number'=>1
            ]);
        }
        exit(json_encode(['code'=>2,'msg'=>'成功','data'=>[]]));
    }


}